<?php

namespace app\api\command;

use app\common\model\OrderMore;
use app\common\model\Packages;
use app\common\model\User;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Orderdaili extends Command
{
    protected function configure()
    {
        $this->setName('order_daili')->setHelp('平台1用户端订单查询')->setDescription('用户端订单查询');

    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel                        = new \app\common\model\Order();
        $userModel                         = new User();
        $where['platform_type']            = 1;
        $where['is_agency']                = 2;
        $where['is_up']                    = 1;
        $where['is_tripartite_settlement'] = 0;
        $order                             = $orderModel->where($where)->whereIn('order_status','-1,1,2,3,4')->order('id desc')->select();
        $currentTimestamp                  = time();
        if ($order) {
            $url           = "http://agent.haoka.bx10086.cn/v1/haoka_api/orders";//办理套餐
            $r             = '';
            $packagesModel = new \app\admin\model\Packages();
            foreach ($order as $k => $v) {
                sleep(2);
                $packageInfo       = $packagesModel->where(['id' => $v['goods_id']])->find();
                $data['appid']     = config('site.ooen_appid');
                $data['timestamp'] = time();
                $data['nonce_str'] = Random::alnum(32);
                $data['order_id']  = $v['order_id'];
                $data['phone']     = $v['shipping_phone'];
                $string            = ascll($data);
                $stringSignTemp    = $string.'&appsecret='.config('site.open_appsecret');
                $data['sign']      = mb_strtoupper(md5($stringSignTemp));
                $res               = httpPost($url, $data);
                $arr               = json_decode($res, true);
                unset($data);
//                $arr['rows'][0]['remark']            = '100';
//                $arr['rows'][0]['order_status']      = 4;
//                $arr['rows'][0]['order_status_text'] = '测试';
                $day_7                 = strtotime(date('Y-m-d', 7 * 86400 + time()));
                if (!empty($arr['rows'][0])) {
                    $rows               = $arr['rows'][0];
                    $up['order_status'] = $rows['order_status'];
                    if (strpos($rows['remark'], '已激活') !== false) {
                        $up['order_status'] = 1;
                    }

                    if (strpos($rows['remark'], '重新提交') !== false) {
                        $up['order_status'] = 0;
                    }
                    if (time() > $v['hexiao_next_time'] && $v['hexiao_next_time'] > 0) {
                        if ($v['is_verification'] == 0) {
                            $up['is_hexiao'] = 3;
                        } else {
                            $up['is_hexiao'] = 6;
                        }
                    }
                    if (strpos($rows['remark'], '100') !== false || strpos($rows['remark'], '可结算')) {
                        $up['order_status']             = 4;
                        $up['is_tripartite_settlement'] = 1;
                        if ($v['is_verification'] == 1) {
                            $up['is_hexiao'] = 5;
                        }else{
                            $up['is_hexiao'] = 2;
                        }
                        if ($packageInfo['day'] != 6) {
                            $up['hexiao_next_time'] = $day_7;
                        } else {
                            $up['hexiao_next_time'] = $up['two_js_time'];
                        }
//                        if ($v['audit_status'] == 2) {
                            $up['order_status']    = 5;
                            $up['settlement_time'] = time();
                            if ($v['is_more'] == 2) {
                                $up['more_num']    = 1;
                                $up['two_js_time'] = settlement_time($up['settlement_time']);//二次结算时间;
                                $huafei            = $packageInfo['bxhf_text'];
                            } else {
                                $huafei = bcadd($packageInfo['ciyue_set_meal'], $packageInfo['bxhf_text']);
//                                if ($packageInfo['one_commission']) {
//                                    $userModel::money($packageInfo['one_commission'], $v['user_id'], '自购奖励',
//                                        $v['id'],
//                                        $packageInfo['id']);
//                                }
//
//                                if ($packageInfo['junior_contribution'] && $v['parent_id']) {
//                                    $userModel::money($packageInfo['junior_contribution'], $v['parent_id'], '直推奖励',
//                                        $v['id'],
//                                        $packageInfo['id']);
//                                }
//
//                                if ($packageInfo['junior_contribution_tow'] && $v['two_id']) {
//                                    $userModel::money($packageInfo['junior_contribution_tow'], $v['two_id'],
//                                        '下下级贡献', $v['id'],
//                                        $packageInfo['id']);
//                                }
//
//                                if ($packageInfo['pplots_yunying_profit'] && $v['plot_id']) {
////                                    $agencyInfo = \app\admin\model\user\Agency::where(['user_id'=>$v['plot_id']])->find();
////                                    if($agencyInfo['money'] > $packageInfo['pplots_yunying_profit']){
////                                        if($agencyInfo->money > $packageInfo['pplots_yunying_profit']){
////                                            $agencyInfo->money -= $packageInfo['pplots_yunying_profit'];
////                                            $agencyInfo->save();
////                                            $packageInfo['pplots_yunying_profit'] = 0;
////                                        }else{
////                                            $packageInfo['pplots_yunying_profit'] = $packageInfo['pplots_yunying_profit'] - $agencyInfo->money;
////                                            $agencyInfo->money = 0;
////                                            $agencyInfo->save();
////                                        }
////                                    }
//                                    if($packageInfo['pplots_yunying_profit']){
//                                        $userModel::money($packageInfo['pplots_yunying_profit'], $v['plot_id'],
//                                            '小区运营分润', $v['id'],
//                                            $packageInfo['id']);
//                                    }
//                                }
//
//                                if ($packageInfo['pplots_profit'] && $v['agency_area_id']) {
////                                    $agencyInfo = \app\admin\model\user\Agency::where(['user_id'=>$v['agency_area_id']])->find();
////                                    if($agencyInfo['money'] > $packageInfo['pplots_profit']){
////                                        if($agencyInfo->money > $packageInfo['pplots_profit']){
////                                            $agencyInfo->money -= $packageInfo['pplots_profit'];
////                                            $agencyInfo->save();
////                                            $packageInfo['pplots_profit'] = 0;
////                                        }else{
////                                            $packageInfo['pplots_profit'] = $packageInfo['pplots_profit'] - $agencyInfo->money;
////                                            $agencyInfo->money = 0;
////                                            $agencyInfo->save();
////                                        }
////                                    }
//                                    if($packageInfo['pplots_profit']){
//                                        $userModel::money($packageInfo['pplots_profit'], $v['agency_area_id'], '区县运营分润',
//                                            $v['id'],
//                                            $packageInfo['id']);
//                                    }
//
//                                }
                            }

                            jiangli($v, $packageInfo);
                            if ($v['parent_id']) {
                                jiangli($v, $packageInfo, 2);
                            }
                            if ($v['two_id']) {
                                jiangli($v, $packageInfo, 3);
                            }
                            //区县
                            if ($v['agency_area_id']) {
                                jiangli($v, $packageInfo, 4);
                            }
                            //小区
                            if ($v['plot_id']) {
                                jiangli($v, $packageInfo, 5);
                            }
                            if ($huafei) {
                                $userModel::money($huafei, $v['user_id'], '话费报销', $v['id'],
                                    $packageInfo['id']);
                                $up['baoxiao_time']   = time();
                                $up['baoxiao_status'] = 2;
                            }
//                        }
                    }
                    if (!empty($v['admin_image'])) {
                        $up['order_status'] = 1;
                    }
                    $up['order_info_json']   = json_encode($arr['rows'], true);
                    $up['order_status_text'] = $rows['order_status_text'] ?? '';
                    $up['rebate']            = $rows['rebate'] ?? '';
                    $up['channel']           = $rows['channel'] ?? '';
                    $up['remark']            = $rows['remark'] ?? '';
                    $up['package_name']      = $packageInfo['name'];
                    if (empty($v['phone'])) {
                        $up['phone'] = $rows['phone'];
                    }
                    if (!empty($rows['shipping_no'])) {
                        $up['shipping_no'] = $rows['shipping_no'];
                    }
                    if (!empty($rows['shipping_company_name'])) {
                        $up['shipping_company_name'] = $rows['shipping_company_name'];
                    }
                    $up['is_recharged'] = $rows['is_recharged'] ?? '';
                    $r                  = db('order')->where(['id' => $v['id']])->update($up);
                }
                if ($r) {
                    echo "操作成功";
                } else {
                    echo "更新失败";
                }
            }
        }
    }

}
